package com.ews.onlineexchange.model;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Lob;
import javax.persistence.PrePersist;
import javax.persistence.Transient;

@Entity
public class Image extends Attachment {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private byte[] thumbnailData;
	private String thumbnailPath;

	//	@NotNull
	@Lob
	@Basic(fetch = FetchType.LAZY)
	public byte[] getThumbnailData() {
		return thumbnailData;
	}

	public void setThumbnailData(byte[] thumbnailData) {
		this.thumbnailData = thumbnailData;
	}

	public String getThumbnailPath() {
		return thumbnailPath;
	}

	public void setThumbnailPath(String thumbnailPath) {
		this.thumbnailPath = thumbnailPath;
	}
	
	@PrePersist
	public void prePersist() {
		if (this.getThumbnailData() == null)
			generateThumbnail();
	}

	@Transient
	public void generateThumbnail() {
//		try {
//			
//			String filePath = config.getProperty("com.ews.onlineExchange.saveDir")+getFilepath();
//			byte[] data = AttachmentUtils.getBytes(filePath);
//			InputStream is = new BufferedInputStream(new ByteArrayInputStream(data));
//			ByteArrayOutputStream os = new ByteArrayOutputStream();
//			Thumbnails.of(is).size(240, 240).outputFormat("png").toOutputStream(os);
//			byte[] thumbnailData = os.toByteArray();
//			this.setThumbnailData(thumbnailData);
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
	}

	public Image() {}

}
